package tum0r.cryptology.encryption;

import tum0r.misc.StringUtils;

/**
 * 工程: JavaTools<br>
 * 包: tum0r.cryptology.encryption<br>
 * 创建者: tum0r<br>
 * 创建时间: 2020/8/21 23:23<br>
 * <br>
 */
public class VirginiaEncryption {
    public String encryption(String plainText, String key) {
        if (StringUtils.isNullOrEmptyOrSpace(plainText) || StringUtils.isNullOrEmptyOrSpace(key)) {
            return null;
        }
        char[] plainTemp = plainText.toUpperCase().replace(" ", "").toCharArray();
        char[] keyTemp = key.toUpperCase().replace(" ", "").toCharArray();
        StringBuilder result = new StringBuilder();
        for (int count = 0; count < plainTemp.length; count++) {
            result.append(keyTemp[count % keyTemp.length]);
        }
        keyTemp = result.toString().toCharArray();
        result = new StringBuilder();
        for (int count = 0; count < plainTemp.length; count++) {
            result.append((char) ((plainTemp[count] + keyTemp[count]) % 26 + 'A'));
            if (count % 5 == 0 && count != 0) {
                result.append(" ");
            }
        }
        return result.toString();
    }
}
